package com.cloud.school.bus.teacherhelper.protocol.login;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import com.cloud.school.bus.teacherhelper.entitys.LoginSetting;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting.AttendanceType;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting.PhotoTag;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting.TeacherDuty;
import com.cloud.school.bus.teacherhelper.protocol.baseresponse.BaseJsonHttpResponse;

public class LoginSettingResponse extends BaseJsonHttpResponse {

	protected LoginSetting loginSetting = new LoginSetting();

	@Override
	public void onSuccess(int statusCode, Header[] headers, String responseString)
			throws Throwable {

		if ("1".equals(code)) {
			JSONObject mainJsonObject = new JSONObject(responseString);
			JSONObject attendanceTypeJsonObject = mainJsonObject
					.optJSONObject("attendance_type");
			if (null != attendanceTypeJsonObject) {
				for (int i = 0; i < attendanceTypeJsonObject.length(); i++) {
					String key = (i + 1) + "";
					AttendanceType attendanceType = new AttendanceType();
					attendanceType.key = key;
					attendanceType.value = attendanceTypeJsonObject.optString(
							key, "");
					loginSetting.attendanceTypeList.add(attendanceType);
				}
			}

			JSONObject teacherDutyJsonObject = mainJsonObject
					.optJSONObject("teacher_duty");
			if (null != teacherDutyJsonObject) {
				for (int i = 0; i < teacherDutyJsonObject.length(); i++) {
					String key = (i + 1) + "";
					TeacherDuty teacherDuty = new TeacherDuty();
					teacherDuty.key = key;
					teacherDuty.value = teacherDutyJsonObject
							.optString(key, "");
					loginSetting.teacherDutyList.add(teacherDuty);
				}
			}

			JSONArray photoTagJsonArray = mainJsonObject
					.optJSONArray("photo_tag");
			if (null != photoTagJsonArray) {
				for (int i = 0; i < photoTagJsonArray.length(); i++) {
					JSONArray itemJsonArray = photoTagJsonArray.optJSONArray(i);
					List<PhotoTag> itemPhotoTags = new ArrayList<LoginSetting.PhotoTag>();
					for (int j = 0; j < itemJsonArray.length(); j++) {
						PhotoTag photoTag = new PhotoTag();
						photoTag.photoTag = itemJsonArray.optString(j, "");
						itemPhotoTags.add(photoTag);
					}
					loginSetting.photoTagList.add(itemPhotoTags);
				}
			}

			loginSetting.clientip = mainJsonObject.optString("clientip", "");
		}
	}

}
